﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Core;


namespace xManagerWebApp.__Membership
{
    public partial class UC_ExpenseApprovalControl : System.Web.UI.UserControl
    {
        


        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            
            if (ExpenseLineID.Value != default(Guid) && DepartmentLevelID.Value != default(Guid))
            {
                using (Core.DAL.DataAccessLayer dc = new Core.DAL.DataAccessLayer())
                {
                    ExpenseLine el = ExpenseLine.Get(dc, ExpenseLineID.Value);
                    this.lb_creator.Text = el.ExpenseReport.User.Shortname;
                    this.expense_line.InstanceId = this.ExpenseLineID;

                    if (HIDEBUTTONS.HasValue && HIDEBUTTONS.Value)
                    {
                        this.approve.Visible = false;
                        this.reject.Visible = false;
                    }
                    else
                    {
                        this.status.Text = "";
                        this.approve.Visible = true;
                        this.reject.Visible = true;
                    }
                }
            }
            else
            {
                this.lb_creator.Text = "Failed to load";
                this.approve.Visible = false;
                this.reject.Visible = false;
            }

            //this.status.Text = "";
        }



        public Guid? ExpenseLineID
        {
            get
            {
                if (ViewState["ExpenseLineID"] != null)
                    return (Guid?)ViewState["ExpenseLineID"];
                else
                    return null;
            }
            set { ViewState["ExpenseLineID"] = value; }
        }


        public bool? HIDEBUTTONS
        {
            get
            {
                if (ViewState["HIDEBUTTONS"] != null)
                    return (bool?)ViewState["HIDEBUTTONS"];
                else
                    return null;
            }
            set { ViewState["HIDEBUTTONS"] = value; }
        }




        public Guid? DepartmentLevelID
        {
            get
            {
                if (ViewState["DepartmentLevelID"] != null)
                    return (Guid?)ViewState["DepartmentLevelID"];
                else
                    return null;
            }
            set { ViewState["DepartmentLevelID"] = value; }
        }

        protected void approve_Click(object sender, EventArgs e)
        {

            ExpenseWorkflow.ExpenseWorkflowContractClient client = null;
            try
            {
                client = new ExpenseWorkflow.ExpenseWorkflowContractClient();                
                this.status.Text = "Approved";
                this.HIDEBUTTONS = true;
                this.TextBox1.Visible = false;
                this.just.Visible = false;

                using (Core.DAL.DataAccessLayer dc = new Core.DAL.DataAccessLayer())
                {
                    NotificationQueue.CloseElementOnQueue(dc, this.ExpenseLineID.Value, this.DepartmentLevelID.Value);
                }
                client.Authorized(ExpenseLineID.Value, DepartmentLevelID.Value, User.CurrentUserId.Value);
            }
            catch (Exception x)
            {
                this.status.Text = "Could not process your request";
            }
            finally
            {
                if (client != null) client.Close();

            }
        }

        protected void reject_Click(object sender, EventArgs e)
        {
            ExpenseWorkflow.ExpenseWorkflowContractClient client = null;
            try
            {
                client = new ExpenseWorkflow.ExpenseWorkflowContractClient();
                this.status.Text = "Rejected";
                this.HIDEBUTTONS = true;
                this.TextBox1.Visible = false;
                this.just.Visible = false;
                using (Core.DAL.DataAccessLayer dc = new Core.DAL.DataAccessLayer())
                {
                    ExpenseLine line = dc.DataContext.ExpenseLines.SingleOrDefault(elem => elem.Id.Equals(this.ExpenseLineID.Value));
                    foreach (ExpenseLine x in line.ExpenseReport.ExpenseLines)
                        NotificationQueue.CloseElementsOnQueue(dc, x.Id);
                }
                client.Rejected(ExpenseLineID.Value, DepartmentLevelID.Value, User.CurrentUserId.Value, this.TextBox1.Text);
            }
            catch (Exception x)
            {
                this.status.Text = "Could not process your request";
            }
            finally
            {
                if (client != null) client.Close();

            }
        }
    }
}